package com.zjw.zy.coreui.fragment.holder

import android.view.View
import android.widget.TextView
import com.zjw.zy.R
import com.zjw.zy.base.BaseViewHolder
import com.zjw.zy.bean.RecorderItem
import com.zjw.zy.coreui.view.VoicePlayView
import com.zjw.zy.ext.alertInput
import com.zjw.zy.utils.TimeUtils

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/10/17 16:40
 */
class RecorderItemHolder(itemView: View) : BaseViewHolder<RecorderItem>(itemView) {

    companion object {
        const val EVENT_DELETE = "EVENT_DELETE"
        const val EVENT_PLAY = "EVENT_PLAY"
        const val EVENT_STOP = "EVENT_STOP"
        const val EVENT_TRANS_TO_TEXT = "EVENT_TRANS_TO_TEXT"
        const val EVENT_RENAME = "EVENT_RENAME"
    }

    private val mTitle: TextView by lazy { findViewById(R.id.recorder_item_title) }
    private val mDate: TextView by lazy { findViewById(R.id.recorder_item_date) }
    private val mDuration: TextView by lazy { findViewById(R.id.recorder_item_duration) }
    private val mVoiceExpand: VoicePlayView by lazy { findViewById(R.id.recorder_item_voice_view) }
    override fun onBindListener() {
        mTitle.setOnClickListener {
            if (isExpand()) {
                unExpand()
            } else {
                expand()
            }
        }
        mTitle.setOnLongClickListener {
            alertInput(title = "重命名", text = data.title) {
                val copy = data.copy()
                copy.title = it
                mListener.onCallbackOtherEvent(EVENT_RENAME, copy, position)
            }
            return@setOnLongClickListener false
        }
        mVoiceExpand.bindListener(object : VoicePlayView.Listener {
            override fun onDeleteClick() {
                mListener?.onCallbackOtherEvent(EVENT_DELETE, data)
            }

            override fun onPlayClick() {
                mListener?.onCallbackOtherEvent(EVENT_PLAY, this@RecorderItemHolder)
            }

            override fun onStopClick() {
                mListener?.onCallbackOtherEvent(EVENT_STOP, data)
            }

            override fun onTransToTextClick() {
                mListener?.onCallbackOtherEvent(EVENT_TRANS_TO_TEXT, data)
            }
        })
    }

    override fun onBindData(data: RecorderItem) {
        data.let {
            mTitle.text = data.title
            mDate.text = data.dateOfString
            mDuration.text = TimeUtils.parseToStrTime(data.duration)

            unExpand()
            mVoiceExpand.bindData(0, data.duration)
        }
    }

    fun updateProgress(currentDuration: Int) {
        mVoiceExpand.bindData(currentDuration, data.duration)
    }

    private fun expand() {
        mVoiceExpand.visibility = View.VISIBLE
    }

    fun unExpand() {
        mVoiceExpand.visibility = View.GONE
    }

    private fun isExpand() = mVoiceExpand.visibility == View.VISIBLE
}
